package com.cloud.pole.rpc;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.api.user.BaseUserApi;
import com.cloud.pole.api.user.UserAuthorizedWxApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.user.service.AuthService;
import com.cloud.pole.user.service.UserService;
import com.cloud.pole.user.service.authorized.UserAuthorizedWxService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/user")
public class UserRest {
	
    @Autowired
    private AuthService authService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserAuthorizedWxService userAuthorizedWxService;
    
    /**
     * 检测用户状态，根据token
     * @param jwtInfo
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/validate", method = RequestMethod.GET)
    public ApiRestResponse<Boolean> validate(String token){
		try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, authService.validate(token));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }

	@RequestMapping(value = "/checkPayPwd", method = RequestMethod.GET)
	public ApiRestResponse<Boolean> checkPayPwd(String token, String payPwd){
		try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			ParamException.err(StringUtils.isBlank(payPwd), "payPwd不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userService.checkPayPwd(token, payPwd));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

    @RequestMapping(value = "/checkUserExistsByMobile", method = RequestMethod.GET)
    public ApiRestResponse<Boolean> checkUserExistsByMobile(String mobileArea, String mobile){   	
    	try {
			ParamException.err(StringUtils.isBlank(mobileArea), "mobileArea不能为空");
			ParamException.err(StringUtils.isBlank(mobile), "mobile不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userService.checkUserExistsByMobile(mobileArea, mobile));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @ResponseBody
    @RequestMapping(value = "/getBaseUser", method = RequestMethod.GET)
    public ApiRestResponse<BaseUserApi> getBaseUser(String token){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userService.getBaseUser(token));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @ResponseBody
    @RequestMapping(value = "/getUserAuthorizedWx", method = RequestMethod.GET)
    public ApiRestResponse<UserAuthorizedWxApi> getUserAuthorizedWx(
    		  @RequestParam(name="token", defaultValue = "") String token,
    		  @RequestParam(name="wxAppId", defaultValue = "") String wxAppId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			ParamException.err(StringUtils.isBlank(wxAppId), "wxAppId不能为空");
			ParamException.err(RegexUtils.isPositiveInteger(wxAppId), "wxAppId错误");

			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userAuthorizedWxService.getUserAuthorizedWxApi(token, Integer.parseInt(wxAppId)));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    /**
     * 根据手机查询用户信息
     * @param mobileArea
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/getUserByMobile", method = RequestMethod.GET)
    public ApiRestResponse<BaseUserApi> getUserByMobile(String mobileArea, String mobile){   	
    	try {
			ParamException.err(StringUtils.isBlank(mobileArea), "mobileArea不能为空");
			ParamException.err(StringUtils.isBlank(mobile), "mobile不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userService.getUserByMobile(mobileArea, mobile));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
	/**
	 * 小程序绑定手机号
	 */
    @RequestMapping(value = "wxUserBind" , method = RequestMethod.GET)
    public ApiRestResponse<BaseUserApi> wxUserBind(String mobile, String mobileArea, String openId, Integer userType) {
        try {
			ParamException.err(StringUtils.isBlank(mobile), "手机号不能为空");
			ParamException.err(StringUtils.isBlank(mobileArea), "手机号归属地不能为空");
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");
			BaseUserApi userVo = authService.wxUserBind(mobile, mobileArea, openId, userType);
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    /**
     * 通过微信openId登录
     * 需要
     * 1、保存微信信息
     * 2、绑定手机
     * 3、授权登录
     * @param openId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "wxLogin" , method = RequestMethod.GET)
    public ApiRestResponse<BaseUserApi> wxLogin(
    		@RequestParam(name = "openId", defaultValue = "") String openId){
        try {
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");
			BaseUserApi userVo = authService.wxLogin(openId);
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    /**
     * 根据营销代码查询用户信息
     * @param marketCode
     * @return
     */
    @RequestMapping(value = "/getUserByMarketCode", method = RequestMethod.GET)
    public ApiRestResponse<BaseUserApi> getUserByMarketCode(String marketCode){   	
    	try {
			ParamException.err(StringUtils.isBlank(marketCode), "marketCode不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userService.getUserByMarketCode(marketCode));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
